package com.blog.admin.filter;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.apache.shiro.session.SessionException;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.filter.authc.LogoutFilter;

import com.blog.admin.pojo.auth.User;
import com.blog.admin.utils.CacheKeys;
import com.blog.admin.utils.RedisUtils;


public class SystemLogoutFilter extends LogoutFilter{
	@Override
    protected boolean preHandle(ServletRequest request, ServletResponse response) throws Exception {
        //在这里执行退出系统前需要清空的数据
		Subject subject = getSubject(request, response);
		
		Object principal = subject.getPrincipals().getPrimaryPrincipal();
		//清空用户权限缓存
		if (principal instanceof User) {
			User user = (User) principal;
			Integer id = user.getId();
			
			RedisUtils.getJedis().hdel(CacheKeys.USER_PERMISSIONS, id.toString()); //删除缓存的数据
		}
        String redirectUrl = getRedirectUrl(request, response, subject);

        try {
            subject.logout();
        } catch (SessionException ise) {
           ise.printStackTrace();
        }
        issueRedirect(request, response, redirectUrl);
        //返回false表示不执行后续的过滤器，直接返回跳转到登录页面
        return false;

    }
}
